#!/usr/bin/env python
# -*- coding: utf-8 -*-
##
##    Copyright (C) 2005 alexisoft (manatlan[at]gmail(dot)com)
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 2 only.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##

from common.template import Template
import feedparser

__title__=u"News RSS"
__order__=3000
__author__ ="alexisoft/manatlan"
__about__  ="permet de naviguer dans ses flux rss"

    
class main:
    def __init__(self):
        # initialisation ...
        if not self.conf.items():
            # il n'y a pas de feeds de configuré
            # on en met, par défaut ...
            liste = [
                ("Le Monde international","http://www.lemonde.fr/rss/sequence/0,2-3210,1-0,0.xml"),
                ("Le Monde france","http://www.lemonde.fr/rss/sequence/0,2-3224,1-0,0.xml"),
                ("Linuxfr","http://linuxfr.org/backend/news-homepage/rss20.rss"),
                ("Linuxfr journaux","http://linuxfr.org/backend/journaux/rss20.rss"),
                ("Freenews","http://www.freenews.fr/feeds/rss.php"),
                ("Ratatium","http://www.ratiatum.com/rss/news.rss"),
                ("Daily python","http://www.pythonware.com/daily/rss2.xml"),
                ("Planet Ubuntu","http://planet.ubuntu-fr.org/rss.php"),
                ("google-news une","http://www.xasa.com/news/xml/fr/top.xml"),
                ("google-news france","http://www.xasa.com/news/xml/fr/nation.xml"),
                ("google-news inter","http://www.xasa.com/news/xml/fr/world.xml")
            ]
            
            c=1
            for titre,url in liste:
               self.conf["feed%03d"%c] = titre,url
               c+=1
            self.save()
        
        
    def index(self):
        array=[]
        feeds = self.conf.keys()
        feeds.sort()
        idx=1
        for key in feeds:
            nom,url = self.conf[key]
            array.append( (key,nom) )
            idx+=1
            
        return Template({"BACK":"/index"})

    def view(self, feed):
        title,url = self.conf[feed]

        rss = feedparser.parse(url)
        array = []
        
        name = rss.feed.title
        
        description = True
        
        i = 0
        for item in rss.entries:
            i += 1
            if not hasattr(item, 'summary'):
                description = False
            
            array.append((str(i), item.title))

        return Template({"BACK":"index"})

    
    def item(self, num, feed):
        title,url = self.conf[feed]
        
        rss = feedparser.parse(url)
        item = rss.entries[int(num)-1]
        if not item:
            return "No RSS item found for num = %s and feed = %s" % (num, feed)
        
        name = item.title
        description = item.summary
        
        return Template({"BACK":"view?feed="+feed})
